﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Linq.Utils;

namespace MyForum.Business.Controllers
{
    public class RoleController : GenericController<Role, MyForumDataContext>
    {
        public static Role GetRole(string rolename)
        {
            #region Validation
            if (String.IsNullOrEmpty(rolename))
            {
                throw new ArgumentNullException("rolename");
            }
            #endregion

            Role role = (from r in DataContext.Roles
                         where r.Rolename == rolename
                         select r).SingleOrDefault();

            return role;
        }

        public static void AddUserToRole(User user, Role role)
        {
            #region Validation
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (role == null)
            {
                throw new ArgumentNullException("role");
            }
            #endregion

            UserInRole membership = new UserInRole { User = user, Role = role };
            UserInRoleController.Insert(membership);
        }

        public static void CreateRole(string roleName)
        {
            #region Validation
            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName");
            }
            #endregion

            Role role = new Role { Rolename = roleName };
            Insert(role);
        }

        public static bool DeleteRole(string roleName)
        {
            #region Validation
            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName");
            }
            #endregion

            Role role = new Role { Rolename = roleName };
            Delete(role);

            return true;
        }

        public static IQueryable<User> FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public static bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public static void RemoveUserFromRole(User user, Role role)
        {
            throw new NotImplementedException();
        }
    }
}
